/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.Dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import web.Dao.OrderDAO;
import web.enity.Item;
import web.enity.Order;
import web.enity.OrderDital;
import web.util.DBUitl;

/**
 *
 * @author 10592
 */
public class OrderDAOImpl implements OrderDAO{

    @Override
    public int insertOrder(Order order) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        int result=0;
        try( Connection con=DBUitl.getConnection())
        {
            PreparedStatement ps=con.prepareStatement("INSERT INTO `order` (userName,createDate) value (?,?)");
//            ps.setInt(1, order.getOid());
            ps.setString(1,order.getUserName() );
            ps.setDate(2, new Date(order.getCreatDate().getTime()));
            result=ps.executeUpdate();
            
            // 将order在数据库中的ID值回写
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select @@IDENTITY;");
            rs.next();
            order.setOid(rs.getInt(1));
            System.out.println("======== " + order.getOid());
        } catch (Exception e) {
        }
        return result;
    }
@Override
    public int insertItems(Item item) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        int result = 0;
        try (Connection con = DBUitl.getConnection()) {
            PreparedStatement ps = con.prepareStatement("INSERT INTO item (item.oid,item.bid,item.count,item.totalPrice) value (?,?,?,?)");
            ps.setInt(1, item.getOid());
            ps.setInt(2, item.getBid());
            ps.setInt(3, item.getCount());
            ps.setString(4, item.getTotalPricel());
           
            result = ps.executeUpdate();
           
        } catch (Exception e) {
        }
        return result;
    } 
    public List<OrderDital> quaryOrderiItems(String userName) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<OrderDital> items = new ArrayList<>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try (Connection con = DBUitl.getConnection()) {
            ps = con.prepareStatement("SELECT * from item LEFT OUTER JOIN `order` ON item.oid=`order`.oid "
                    + "LEFT OUTER JOIN book on item.bid=book.bid "
                    + "WhERE `order`.userName=?");
            ps.setString(1, userName);
            rs = ps.executeQuery();
            while (rs.next()) {
                OrderDital item = ecapsulate1(rs);
                items.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

    private OrderDital ecapsulate1(ResultSet rs) throws SQLException {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
          OrderDital od=new OrderDital();
          od.setBookImage(rs.getString("images"));
          od.setCreateDate(rs.getDate("createDate"));
          od.setOid(rs.getInt("oid"));
          od.setTotalPrice(rs.getString("totalPrice"));
          od.setUserName(rs.getString("userName"));
          return od;
    }

  

    public static void main(String[] args) {
        OrderDAO odao=new OrderDAOImpl();
        List<OrderDital> ods=odao.quaryOrderiItems("111");
        for (OrderDital od : ods) {
            System.out.println(od.getUserName()+"\t"+od.getBookImage() + "\t" +od.getCreateDate()+"\t"+od.getOid());
        }
    }
}
